package egoc

import (
	"fmt"
	"net/url"
)

const (
	contentType        = "Content-Type"
	contentDisposition = "Content-Disposition"
)

// SetResponseContentDispositionAttachment 设置下载文件的文件名称
func (c *Context) SetResponseContentDispositionAttachment(filename string) {
	c.Writer.Header().Set(contentDisposition,
		fmt.Sprintf("attachment;filename*=UTF-8''%s", url.QueryEscape(filename)))
}

// SetResponseNoCahe 设置浏览器不缓存
func (c *Context) SetResponseNoCahe() {
	c.Writer.Header().Set("Pragma", "No-cache")
	c.Writer.Header().Set("Cache-Control", "No-cache")
	c.Writer.Header().Set("Expires", "0")
}

func (c *Context) SetResponseContentType(contentType string) {
	c.Writer.Header().Set(contentType, contentType)
}

// SetResponseImage 设置返回类型为图片，在浏览器内显示
func (c *Context) SetResponseImage() {
	c.SetResponseContentType("image/*")
	c.Writer.Header().Set(contentDisposition, "inline")
}

// SetResponseDownload 设置返回类型为二进制，并且是下载文件
func (c *Context) SetResponseDownload(filename string) {
	c.SetResponseContentType("application/octet-stream")
	c.SetResponseContentDispositionAttachment(filename)
}
